package com.song.springclouddemoshardingjdbc.controller;
//
//
//import com.song.springclouddemoshardingjdbc.utils.ImageUtils;
//
//import javax.imageio.ImageIO;
//import java.awt.image.BufferedImage;
//import java.io.IOException;
//import java.io.File;
//import java.net.URL;
//
//
//public class TestController {
//
//    public static void main(String[] args) {
//        try {
//            //通过url获取BufferedImage图像缓冲区
//            //URL img = new URL("https://s2.loli.net/2023/12/14/ShOoytfRnrCakse.jpg");
//            URL img = new URL("https://gitee.com/song-rice/upload-image/raw/2bb7a342209089bf38fc971851f25d5ba77e7ce6/%E7%99%BD%E5%BA%95%E8%AF%81%E4%BB%B6%E7%85%A7_215x300.jpg");  //证件照
//            BufferedImage image = ImageIO.read(img);
//            //获取图片的宽、高
//            System.out.println("Width: " + image.getWidth());
//            System.out.println("Height: " + image.getHeight());
//            //调整图片大小为 400X400尺寸
//            BufferedImage newImage = ImageUtils.resizeImage(image, 215, 300);
//            //将缓冲区图片保存到 F:/test/pic1.jpg (文件不存在会自动创建文件保存，文件存在会覆盖原文件保存)
//            ImageIO.write(newImage, "jpg", new File("F:/pic3.jpg"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//}


import com.song.springclouddemoshardingjdbc.utils.ImageUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.File;
import java.net.URL;

import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

public class TestController {


    public static void main(String[] args) {
        try {
            // 通过URL获取BufferedImage图像缓冲区
            URL img = new URL("https://gitee.com/song-rice/upload-image/raw/2bb7a342209089bf38fc971851f25d5ba77e7ce6/%E7%99%BD%E5%BA%95%E8%AF%81%E4%BB%B6%E7%85%A7_215x300.jpg");  //证件照
            BufferedImage image = ImageIO.read(img);

            // 调整图片大小为 215x300尺寸
            BufferedImage resizedImage = ImageUtils.resizeImage(image, 215, 300);

            // 压缩图片到目标文件大小
            File compressedFile = compressImage(resizedImage, 30);

            // 输出压缩后图片的文件大小
            long fileSizeInBytes = compressedFile.length();
            System.out.println("Compressed Image File Size: " + fileSizeInBytes + " bytes");

            // 将修改后的文件输出到当前用户桌面
            File desktop = new File(System.getProperty("user.home"), "Desktop");
            File output = new File(desktop, "compressed.jpg");
            Files.copy(compressedFile.toPath(), output.toPath(), StandardCopyOption.REPLACE_EXISTING);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /*
        压缩图片
     */
    private static File compressImage(BufferedImage image, int targetFileSizeKb) throws IOException {
        double targetFileSizeBytes = targetFileSizeKb * 1024;  // 目标文件大小（字节）

        double compressionQuality = 1.0;  // 初始压缩质量
        double maxCompressionQuality = 0.1;  // 最大压缩质量

        File compressedFile = null;
        while (compressionQuality > maxCompressionQuality) {
            // 创建临时文件
            compressedFile = File.createTempFile("compressed", ".jpg");
            // 将图像保存到临时文件中
            ImageIO.write(image, "jpg", compressedFile);

            // 计算当前压缩后的文件大小
            long fileSizeInBytes = compressedFile.length();

            // 如果当前文件大小小于等于目标大小，则结束压缩循环
            if (fileSizeInBytes <= targetFileSizeBytes) {
                break;
            }

            // 调整压缩质量
            compressionQuality -= 0.1;
            ImageUtils.compressImageWithQuality(image, compressedFile, compressionQuality);
        }

        return compressedFile;
    }
}
